package com.compass.doctor.dialog

import android.app.Dialog
import android.os.Build
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.fragment.app.FragmentActivity
import com.compass.doctor.R
import com.compass.doctor.databinding.DialogRefusalBinding
import com.compass.doctor.ui.WebDetailActivity
import com.compass.framework.base.BaseDialog
import com.compass.framework.base.BaseDialogFragment
import com.compass.framework.databinding.DialogMessageBinding
import com.compass.framework.dialog.CommonMessageDialog
import com.compass.framework.ext.onClick
import com.compass.framework.manager.AppManager
import com.compass.framework.toast.TipsToast
import com.compass.framework.utils.LogUtil
import com.compass.framework.utils.getColorFromResource
import com.compass.framework.utils.getStringFromResource

/**
 * Description： 协议弹窗
 * Date:2024/5/14 17:29
 */
class AgreementMessageDialog {
    class Builder(activity: FragmentActivity) : BaseDialogFragment.Builder<Builder>(activity) {
        private val mBinding: DialogMessageBinding =
            DialogMessageBinding.inflate(LayoutInflater.from(activity))

        init {
            initView()

        }

        private fun initView() {
            setContentView(mBinding.root)
            setWidth((AppManager.getScreenWidthPx() * 0.8).toInt())
            setAnimStyle(BaseDialog.AnimStyle.DEFAULT)
            setGravity(Gravity.CENTER)
            initAgreement()

        }
        fun setonConfirmListener(onConfirm: (dialog: Dialog?) -> Unit): AgreementMessageDialog.Builder {
            mBinding.tvDialogMessageConfirm.onClick {
                onConfirm?.invoke(dialog)
            }
            return this
        }

        fun setonCancelListener(onCancel: (dialog: Dialog?) -> Unit): AgreementMessageDialog.Builder {
            mBinding.tvDialogMessageCancel.onClick {
                onCancel?.invoke(dialog)
            }
            return this
        }

        fun setTitle(text: CharSequence?): AgreementMessageDialog.Builder {
            mBinding.tvDialogMessageTitle.text = text
            return this
        }

        fun setConfirm(text: CharSequence?): AgreementMessageDialog.Builder {
            mBinding.tvDialogMessageConfirm.text = text
            return this
        }

        fun setCancel(text: CharSequence?): AgreementMessageDialog.Builder {
            mBinding.tvDialogMessageCancel.text = text
            val isEmpty = text.isNullOrEmpty()
            mBinding.tvDialogMessageCancel.visibility = if (isEmpty) View.GONE else View.VISIBLE
            mBinding.vDialogMessageLine.visibility = if (isEmpty) View.GONE else View.VISIBLE
            mBinding.tvDialogMessageConfirm.setBackgroundResource(if (isEmpty) com.compass.framework.R.drawable.dialog_message_one_button else com.compass.framework.R.drawable.dialog_message_right_button)
            return this
        }


        /**
         * 初始化协议点击
         */
        private fun initAgreement() {
            val agreement = getStringFromResource(R.string.dialog_login_agreement)
            try {
                //设置点击事件，加上这句话才有效果
                mBinding.tvDialogMessageMessage.movementMethod = LinkMovementMethod.getInstance()
                val spaBuilder = SpannableStringBuilder(agreement)
                val privacySpan = getStringFromResource(com.compass.framework.R.string.login_privacy_agreement)
                val serviceSpan = getStringFromResource(com.compass.framework.R.string.login_user_agreement)
                spaBuilder.setSpan(
                    object : ClickableSpan() {
                        override fun onClick(widget: View) {
                            (widget as TextView).highlightColor =
                                getColorFromResource(com.compass.framework.R.color.transparent)

                            WebDetailActivity.start(
                                context = activity,
                                url = com.compass.framework.constant.POLICY,
                                title = "隐私政策")

                        }

                        @RequiresApi(Build.VERSION_CODES.M)
                        override fun updateDrawState(ds: TextPaint) {
                            super.updateDrawState(ds)
                            ds.color = getColorFromResource(com.compass.framework.R.color.color_0165b8)
                            ds.isUnderlineText = false
                            ds.clearShadowLayer()
                        }
                    },
                    spaBuilder.indexOf(privacySpan),
                    spaBuilder.indexOf(privacySpan) + privacySpan.length,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                )
                spaBuilder.setSpan(
                    object : ClickableSpan() {
                        override fun onClick(widget: View) {
                            (widget as TextView).highlightColor =
                                getColorFromResource(com.compass.framework.R.color.transparent)
                            WebDetailActivity.start(
                                context = activity,
                                url = com.compass.framework.constant.SERVICE,
                                title = "服务协议"
                            )
                        }

                        @RequiresApi(Build.VERSION_CODES.M)
                        override fun updateDrawState(ds: TextPaint) {
                            super.updateDrawState(ds)
                            ds.color = getColorFromResource(com.compass.framework.R.color.color_0165b8)
                            ds.isUnderlineText = false
                            ds.clearShadowLayer()
                        }
                    },
                    spaBuilder.indexOf(serviceSpan),
                    spaBuilder.indexOf(serviceSpan) + serviceSpan.length,
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                )
                mBinding.tvDialogMessageMessage.setText(spaBuilder, TextView.BufferType.SPANNABLE)
            } catch (e: Exception) {
                LogUtil.e(e)
                mBinding.tvDialogMessageMessage.text = agreement
            }
        }
    }
}
